<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日报ID" prop="dailyReportId">
        <el-input
          v-model="queryParams.dailyReportId"
          placeholder="请输入日报ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable style="width: 90px;">
          <el-option
            v-for="dict in issue_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-select v-model="queryParams.priority"  clearable style="width: 90px;">
          <el-option
            v-for="dict in issue_priority"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
           type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['media:issues:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['media:issues:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['media:issues:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['media:issues:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="issuesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" prop="id" />
      <el-table-column label="日报ID" align="center" prop="dailyReportId" />
      <el-table-column label="问题描述" align="center" prop="issueDescription" />
      <el-table-column label="问题状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="issue_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="问题优先级" align="center" prop="priority">
        <template #default="scope">
          <dict-tag :options="issue_priority" :value="scope.row.priority"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary"  @click="handleInfo(scope.row)" v-hasPermi="['media:issues:query']">查看详情</el-button>
          <el-button link type="primary"  @click="handleUpdate(scope.row)" v-hasPermi="['media:issues:edit']">修改</el-button>
          <el-button link type="primary"  @click="handleDelete(scope.row)" v-hasPermi="['media:issues:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改日报问题详情对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="issuesRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日报ID" prop="dailyReportId">
          <el-input v-model="form.dailyReportId" placeholder="请输入日报ID" />
        </el-form-item>
        <el-form-item label="问题描述" prop="issueDescription">
          <el-input 
          v-model="form.issueDescription" 
          type="textarea" 
          :rows="5"
          placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="问题图片" prop="issueImageUrl">
          <image-upload v-model="form.issueImageUrl"/>
        </el-form-item>
        
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择问题状态">
            <el-option
              v-for="dict in issue_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-select v-model="form.priority" placeholder="请选择问题优先级">
            <el-option
              v-for="dict in issue_priority"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="解决方案" prop="solutionDescription">
          <el-input 
          v-model="form.solutionDescription" 
          type="textarea" 
          :rows="3"
          placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="解决图片" prop="solutionImageUrl">
          <image-upload v-model="form.solutionImageUrl"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!--查看详情对话框 -->
    <el-dialog :title="title" v-model="detailOpen" width="500px" append-to-body>
      <el-form ref="issuesRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日报ID" prop="dailyReportId">
          {{form.dailyReportId}}
        </el-form-item>
        <el-form-item label="问题描述" prop="issueDescription">
          <p v-html=" form.issueDescription "></p>
        </el-form-item>
        <el-form-item label="问题图片" prop="issueImageUrl">
            <el-image
              :preview-src-list="[form.issueImageUrl]"
              :src="form.issueImageUrl"
              style="max-width: 100%; height: auto;"
            />
        </el-form-item>
        
        <el-form-item label="状态" prop="status">
          <div v-for="dict in issue_status" :key="dict.value">
            <span v-if="dict.value == form.status">
              {{dict.label}}
            </span>
          </div>
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <div v-for="dict in issue_priority" :key="dict.value">
            <span v-if="dict.value == form.priority">
              {{dict.label}}
            </span>
          </div>
        </el-form-item>
        <el-form-item label="解决方案" prop="solutionDescription">
          <p v-html=" form.solutionDescription "></p>
        </el-form-item>
        <el-form-item label="解决图片" prop="solutionImageUrl">
          <el-image
              :preview-src-list="[form.solutionImageUrl]"
              :src="form.solutionImageUrl"
              style="max-width: 100%; height: auto;"
            />
        </el-form-item>
      </el-form>
      
    </el-dialog>
  </div>
</template>

<script setup name="Issues">
import { Picture as IconPicture } from '@element-plus/icons-vue'
import { listIssues, getIssues, delIssues, addIssues, updateIssues } from "@/api/media/issues";

const { proxy } = getCurrentInstance();
const { issue_priority, issue_status } = proxy.useDict('issue_priority', 'issue_status');

const issuesList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dailyReportId: null,
    issueDescription: null,
    status: null,
    priority: null,
    createTime: null,
  },
  rules: {
    dailyReportId: [
      { required: true, message: "关联到tb_dailyReports表的日报ID不能为空", trigger: "blur" }
    ],
    issueDescription: [
      { required: true, message: "问题描述不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "问题状态不能为空", trigger: "blur" }
    ],
    priority: [
      { required: true, message: "问题优先级不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询日报问题详情列表 */
function getList() {
  loading.value = true;
  listIssues(queryParams.value).then(response => {
    issuesList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  detailOpen.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    dailyReportId: null,
    issueDescription: null,
    solutionDescription: null,
    status: null,
    priority: null,
    issueImageUrl: null,
    solutionImageUrl: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("issuesRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加日报问题详情";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getIssues(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改问题";
  });
}

/**查看详情按钮操作 */
const detailOpen = ref(false);
function handleInfo(row){
  reset();
  const _id = row.id 
  getIssues(_id).then(response => {
    form.value = response.data;
    detailOpen.value = true;
    title.value = "查看问题详情";

  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["issuesRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateIssues(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addIssues(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除日报问题详情编号为"' + _ids + '"的数据项？').then(function() {
    return delIssues(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('media/issues/export', {
    ...queryParams.value
  }, `issues_${new Date().getTime()}.xlsx`)
}

getList();
</script>
